use chrono::NaiveDateTime;
use sqlx::postgres::PgPoolOptions;
use std::io;

#[derive(Debug)]
pub struct Course {
  pub id: i32,
  pub teacher_id: i32,
  pub name: String,
  pub time: Option<NaiveDateTime>,
}

#[actix_web::main]
async fn main() -> io::Result<()> {
  let database_url = "postgres://postgres:postgres@127.0.0.1:5432/test_demo";

  let db_pool = PgPoolOptions::new().connect(database_url).await.unwrap();

  // 这一这里使用了预编译的宏，需要配置 .env 的数据路正确路径
  let course_rows = sqlx::query!(
    r#"
    select * from course where id = $1
  "#,
    3
  )
  .fetch_all(&db_pool)
  .await
  .unwrap();

  let mut courses_list = vec![];
  for row in course_rows {
    courses_list.push(Course {
      id: row.id,
      teacher_id: row.teacher_id,
      name: row.name,
      time: Some(row.time.unwrap()),
    });
  }

  println!("Course: {:#?}", courses_list);

  Ok(())
}
